﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BeniBastanYaratWinFormProject
{
    public partial class satıs : Form
    {
        public satıs()
        {
            InitializeComponent();
            Hazirlik();
        }
        DataTable dt;
        private void Hazirlik()
        {
            dt = new DataTable();
            DataColumn dc = new DataColumn("Barkod");
            dc.ReadOnly = true;
            dt.Columns.Add(dc);
            dc = new DataColumn("UrunAdi");
            dc.ReadOnly = true;
            dt.Columns.Add(dc);
            dc = new DataColumn("Stok Aciklama");
            dc.ReadOnly = true;
            dt.Columns.Add(dc);
            dc = new DataColumn("Miktar");
            dt.Columns.Add(dc);
            dc = new DataColumn("Fiyat");
            dc.ReadOnly = true;
            dt.Columns.Add(dc);
            dc = new DataColumn("KDV Orani");
            dc.ReadOnly = true;
            dt.Columns.Add(dc);
            dc = new DataColumn("Toplam Tutar");

            dt.Columns.Add(dc);

            txtbarkod.Focus();
        }
        int miktar = 1;
        

        dbbaglantiDataContext db = new dbbaglantiDataContext();
        private void button2_Click(object sender, EventArgs e)
        {
            bool yeni = true;

            foreach (DataGridViewRow items in dataGridView1.Rows)
            {
                try
                {
                    if (txtbarkod.Text == items.Cells[0].Value.ToString())
                    {
                        int adet = Convert.ToInt32(items.Cells[3].Value) + 1;
                        items.Cells[3].Value = adet;
                        yeni = false;

                    }

                }
                catch (Exception)
                {

                    txtbarkod.Text = "";
                    txtbarkod.Focus();

                }
            }
            if (yeni)
            {
                urunbul(Keys.Enter);
            }
        
        }

        private void txtbarkod_KeyPress(object sender, KeyPressEventArgs e)
        {

            if ((Keys)e.KeyChar == Keys.Enter)
            {
                 bool yeni = true;

            foreach (DataGridViewRow items in dataGridView1.Rows)
            {
                try
                {
                    if (txtbarkod.Text == items.Cells[0].Value.ToString())
                    {
                        int adet = Convert.ToInt32(items.Cells[3].Value) + 1;
                        items.Cells[3].Value = adet;
                        yeni = false;
                        
                    txtbarkod.Text = "";
                    txtbarkod.Focus();
                    }

                }
                catch (Exception)
                {

                

                }
            }
            if (yeni)
            {
                urunbul((Keys)e.KeyChar);
            }
            }
        
         
        }

        private void urunbul(Keys e)
        {

           
                

                    try
                    {
                        if (e == Keys.Enter)
                        {

                            var sorgu = (from x in db.Urunlers where x.UrunBarkod == txtbarkod.Text select x).SingleOrDefault();
                            txtbarkod.Text = "";
                            txtbarkod.Focus();


                            DataRow dr = dt.NewRow();
                            dr[0] = sorgu.UrunBarkod;
                            dr[1] = sorgu.UrunAdi;
                            dr[2] = sorgu.UrunAciklamasi;
                            dr[3] = miktar;
                            dr[4] = sorgu.UrunSatisFiyati;
                            dr[5] = sorgu.KDVOrani;
                            dr[6] = miktar * sorgu.UrunSatisFiyati;
                            dt.Rows.Add(dr);

                            dataGridView1.DataSource = dt;
                            toplamtutar = 0;
                            foreach (DataGridViewRow item in dataGridView1.Rows)
                            {
                                fiyathesap(item.Index, false, 0);
                            }

                        }
                    }
                    catch (Exception)
                    {

                    }

                
            


        }

        decimal toplamtutar = 0;
        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 3)
            {
                fiyathesap(e.RowIndex, true,0);
            }
        }

        private void fiyathesap(int e, bool veriDegisti, decimal silme)
        {
            try
            {
                if (silme == 0)
                {
                    decimal adet = Convert.ToDecimal(dataGridView1.Rows[e].Cells[3].Value);
                    decimal fiyat = Convert.ToDecimal(dataGridView1.Rows[e].Cells[4].Value);
                    dataGridView1.Rows[e].Cells[6].Value = (adet * fiyat);
                    
                    if (veriDegisti)
                    {
                        toplamtutar += fiyat;
                    }
                    else
                    {
                        toplamtutar += adet * fiyat;
                    }

                    
                }
                else
                {
                  
                }
            }
            catch (Exception)
            {

            }
        }

        private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            try
            {
                if (silinmeTutari > 0)
                {
                    fiyathesap(e.RowCount, false, silinmeTutari);
                    silinmeTutari = 0;

                }
           
            }
            catch (Exception)
            {
                
             
            }
        }
        decimal silinmeTutari = 0;
        private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            try
            {
                   decimal adet = Convert.ToDecimal(e.Row.Cells[3].Value);
            decimal fiyat = Convert.ToDecimal(e.Row.Cells[4].Value);
            decimal d = (adet * fiyat);
            silinmeTutari = d;
            }
            catch (Exception)
            {
                
            }

        }

        private void btnsatis_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow item in dataGridView1.Rows)
            {
                stokeksilt(item.Index);
                satısekle(item.Index);
            }
            
        }

        private  void satısekle(int e)
        {

            string barkotno = Convert.ToString(dataGridView1.Rows[e].Cells[0].Value);
            var sorgu = (from x in db.Urunlers where x.UrunBarkod == barkotno select x).SingleOrDefault();

            if (sorgu != null)
            {
                Satislar satıs = new Satislar();

                satıs.urunid = Convert.ToInt32(sorgu.UrunID);
                satıs.adet = Convert.ToInt32(dataGridView1.Rows[e].Cells[3].Value);
                satıs.barkot = barkotno;
                satıs.birimfiyat = Convert.ToDecimal(dataGridView1.Rows[e].Cells[4].Value);
                satıs.toplamfiyat = Convert.ToDecimal(dataGridView1.Rows[e].Cells[6].Value);
                satıs.satıstarih = DateTime.Now;

                db.Satislars.InsertOnSubmit(satıs);
                db.SubmitChanges();

            }




        }

        private void stokeksilt(int e)
        {

            string barkotno = Convert.ToString(dataGridView1.Rows[e].Cells[0].Value);
            decimal adet = Convert.ToDecimal(dataGridView1.Rows[e].Cells[3].Value);
            

            var sorgu = (from x in db.Urunlers where x.UrunBarkod == barkotno select x).SingleOrDefault();
            if (adet>0)
            {
                 decimal stok = sorgu.UrunStok - adet;

            sorgu.UrunStok = stok;
            db.SubmitChanges();
            }


            





        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

           // var sorgu = (from x in db.Urunlers where x.UrunBarkod == barkotno select x).SingleOrDefault();

        }

        private void txtbarkod_TextChanged(object sender, EventArgs e)
        {

        }



    }
}
